/*
	Copyright (c) 2005 Peter Moore

	Permission is hereby granted, free of charge, to any person obtaining a copy of
	this software and associated documentation files (the "Software"), to deal in the
	Software without restriction, including without limitation the rights to use,
	copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
	Software, and to permit persons to whom the Software is furnished to do so,
	subject to the following conditions:

	The above copyright notice and this permission notice shall be included,
	unmodified, in all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
	FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
	COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
	AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
	WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#ifndef __DME_IMPL_H__
#define __DME_IMPL_H__

#include "dme.h"

#ifdef WIN32
#	include <windows.h>
#else
#	include <pthread.h>
#	include <X11/Xlib.h>
#	include <gl/glx.h>
#endif

#include <gl/gl.h>

using DarkMatter::Engine;
using DarkMatter::ThreadProc;
using DarkMatter::Thread;
using DarkMatter::Mutex;
using DarkMatter::Screen;
using DarkMatter::Graphics;

namespace DarkMatter
{
	class Thread_Impl: public Thread
	{
		public:
			Thread_Impl(Engine* eng, ThreadProc proc, void* data);
			virtual ~Thread_Impl();
			virtual void Delete();
			virtual void Wait();
			virtual void Kill();

		private:
			Engine* cur_eng;
#ifdef WIN32
			DWORD threadID;
			HANDLE thread;
#else
			pthread_t thread;
#endif
	};

	class Mutex_Impl: public Mutex
	{
		public:
			Mutex_Impl(Engine* eng);
			virtual ~Mutex_Impl();
			virtual void Delete();
			virtual void Lock();
			virtual void Unlock();

		private:
			Engine* cur_eng;
#ifdef WIN32
			CRITICAL_SECTION cs;
#else
			pthread_mutex_t mutex;
#endif
	};

	class Screen_Impl: public Screen
	{
		friend class Graphics_Impl;

		public:
			Screen_Impl(Engine* eng, const char* title, int width, int height, bool fullscreen);
			virtual ~Screen_Impl();
			virtual void Delete();
			virtual void FreeGraphics();
			virtual void PumpEvents();
			virtual Graphics* CreateGraphics(int cbits, int zbits, int sbits, int abits);

		private:
			Graphics* gfx;
			int w, h;
			bool fscreen;
			Engine* cur_eng;
#ifdef WIN32
			HWND hWnd;
#else
			Display* disp;
#endif
	};

	class Graphics_Impl: public Graphics
	{
		public:
			Graphics_Impl(	Screen* scr, int width, int height,
							int cbits, int zbits, int sbits, int abits, bool fullscreen);
			virtual ~Graphics_Impl();
			virtual void Delete();
			virtual void Present();
			virtual void Clear();
			virtual void ClearColor(unsigned char red, unsigned char green, unsigned char blue, unsigned char alpha);
			virtual void ClearStencil(int stencil);
			virtual void ClearAccum(float red, float green, float blue, float alpha);
			virtual void View2D();
			virtual void View3D();
			virtual void DrawPrimitive(PrimitiveType ptype, unsigned int vcount, const Vertex* verts);

		private:
			Screen* screen;
			bool full;
			int w, h;
#ifdef WIN32
			HGLRC hGLRC;
			HDC hDC;
#else
			Window wnd;
			GLXContext ctx;
#endif
	};
}

#endif
